'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ 
    port: 8888
});

server.route({
    method: 'GET',
    path:'/auth',
    handler: function (request, reply) {
        const headers = request.headers;
        const sign = headers['x-yc-sign'];
        const token = headers['x-yc-token'];
        const reToken = headers['x-yc-retoken'];
        request.log(['test'], headers);
        if (sign !== 'yc') {
            return reply({
                errorCode: 17001,
                msg: 'sign错误'
            }).code(498)
        }
        if (token !== '1') {
            if (reToken) {
                return reply({
                    token: '1',
                    retoken: '2222'
                }).code(200)
            } else {
                return reply({
                    errorCode: 17003,
                    msg: 'retoken超时'
                }).code(500)
            }
        }
        return reply().code(204);
    }
});

server.route({
    method: 'GET',
    path:'/test',
    handler: function (request, reply) {
        request.log(['test'], request);
        return reply({code: 200});
    }
});

server.start((err) => {
    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

server.on('request', function(request, event, tags) {
    console.log(event.data);
});